package com.tjsinfo.tjsoa.addresslist.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.UUID;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.Dao.IJDBCDao;

public class AddresslistSendBo implements IAddresslistSendBo {
	private IJDBCDao jdbcDao;
	private IHQLDao hqlDao;
	public IHQLDao getHqlDao() {
		return hqlDao;
	}

	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}

	public IJDBCDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	
/*	public String addListUsersJDBC(String senduserids,String sendListUserids,int id) {
		String[] sendListUserid = sendListUserids.split(",");
		String[] senduseri = senduserids.split(",");
		String str = "";
		for (int i = 0; i < senduseri.length; i++) {
			for (int j = 0; j < sendListUserid.length; j++) {
				String sql = "insert into tjsoadba.Addresslist_Send values('"+id+"','"+senduseri[i]+"','"+sendListUserid[j]+"');";
				jdbcDao.executeUpdate(sql);
				String sql1 = "select max(id) from tjsoadba.Addresslist_Send;";
				ResultSet rs = jdbcDao.executeQuary(sql1);
				try {
					rs.next();
					int id1 = rs.getInt(1);
					str += String.valueOf(id1)+",";
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		jdbcDao.closeRs();
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
		return str.substring(0,str.length()-1);
	}*/
	
	public List getSendUsersByIds(String ids,int userid) {
		String sql = "from AddresslistSend where id in("+ids+") and target_id="+userid+"";
		List list = hqlDao.executeList(sql);
		return list;
	}
	
	public void deleteSendUsers(String sendUsersId,int userid) {
		String sql = "delete from dbo.t_addresslist_Send where id in("+sendUsersId+") and target_id="+userid+"";
		jdbcDao.executeUpdate(sql);
	}
	
	public String addListUsersJDBC(String senduserids,String sendListUserids,int id) {
		String[] sendListUserid = sendListUserids.split(",");
		String[] senduseri = senduserids.split(",");
		Connection conn = jdbcDao.getConn();
		String stmtSQL = "insert into dbo.t_addresslist_Send(id,resource_id,target_id,addresslist_id) values(?,?,?,?)";   
		String str = "";
		ResultSet rs = null;
		try {
			PreparedStatement statement = conn.prepareStatement(stmtSQL,Statement.RETURN_GENERATED_KEYS);
			for (int i = 0; i < senduseri.length; i++) {
				for (int j = 0; j < sendListUserid.length; j++) {
					UUID uuid = UUID.randomUUID();
	             	statement.setString(1, uuid.toString().replace("-", ""));
	             	str += uuid.toString().replace("-", "")+",";
	             	statement.setInt(2, id);   
	             	statement.setInt(3, Integer.valueOf(senduseri[i]));   
	             	statement.setString(4, sendListUserid[j]);   
	             	statement.execute(); 
	             	rs = statement.getGeneratedKeys();
				}
             }
			rs.close();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
}
